
Partial Class SAD_SQWaitStatesE
    Inherits System.Web.UI.Page

    Private Const Q As String = Chr(34)

    Private moServer As SQLAutoDocLib.Server = Nothing
    Private moDatabase As SQLAutoDocLib.Database = Nothing

    Private msDBID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        msDBID = Request.QueryString("D")

        moDatabase = New SQLAutoDocLib.Database(msDBID)
        moDatabase.Load()

        moServer = New SQLAutoDocLib.Server(moDatabase.ServerID)
        moServer.Load()

        divZoom.Visible = False
        ARefresh.HRef = "SQWaitStatesE.aspx?S=" & moDatabase.ServerID & "&D=" & moDatabase.ID
    End Sub

    Protected Function FormLabel() As String
        Dim oFactory As New SQLAutoDocLib.ScanHistory_Factory
        Dim sCurrentScan As String = ""
        Dim sLastScan As String = ""
        oFactory.GetTimeOfLastScan(moDatabase.ID, SQLAutoDocLib.ScanSchedule.Type.WaitStateScan, "SQLAutoDocLib.WaitStateLog", sCurrentScan, sLastScan)

        Dim sValue As String = "Wait State Trends for database " & moDatabase.Name & ". Last scan completed " & sLastScan

        If sCurrentScan > "" Then
            sValue = " scan in progress started " & sCurrentScan
        End If

        Return sValue
    End Function

    Protected Function ScanMode() As String
        Return "Size"
    End Function

    Protected Function EditMode() As String
        Return "Size"
    End Function

    Protected Function ServerID() As String
        Return moServer.ID
    End Function

    Protected Sub ShowWaitStates()
        Dim oFactory As New SQLAutoDocLib.WaitStateLog_Factory
        Dim oData As DataTable = oFactory.WaitState10PointTrends(moDatabase.ID, DateAdd(DateInterval.Weekday, -10, Now), Now)

        Dim oHTM As New StringBuilder

        With oHTM
            .Append("<table style=" & Q & "width: 515px; border-collapse:collapse;" & Q & ">")

            .Append("<tr class=" & Q & "wmObjectList_TH" & Q & " style=" & Q & "background-color: #C3C4C1;" & Q & ">")
            .Append("<td style=" & Q & "width: 200px; border-bottom: solid 1px black;" & Q & ">Wait State</td>")
            .Append("<td style=" & Q & "width: 105px; border-bottom: solid 1px black;" & Q & ">AvgRequests</td>")
            .Append("<td style=" & Q & "width: 105px; border-bottom: solid 1px black;" & Q & ">AvgWaitTime</td>")
            .Append("<td style=" & Q & "width: 105px; border-bottom: solid 1px black;" & Q & ">AvgSignalTime</td>")
            .Append("</tr>")

            Dim bAlternatingRow As Boolean = False

            For Each oRow As DataRow In oData.Rows
                Dim sAvgRequests_URL As String = MakeChartString_AverageRequests(oRow, bAlternatingRow)
                Dim sAvgWaitType_URL As String = MakeChartString_AvgWaitTime(oRow, bAlternatingRow)
                Dim sAvgSignalTime_URL As String = MakeChartString_AvgSignalTime(oRow, bAlternatingRow)

                If sAvgRequests_URL > "" _
                    OrElse sAvgWaitType_URL > "" _
                    OrElse sAvgSignalTime_URL > "" Then

                    If bAlternatingRow = False Then
                        .Append("<tr class=" & Q & "wmObjectList_T" & Q & " style=" & Q & "height:20px;" & Q & ">")
                    Else
                        .Append("<tr class=" & Q & "wmObjectList_TA" & Q & " style=" & Q & "height:20px;" & Q & ">")
                    End If

                    .Append("<td>")
                    .Append("<a href=" & Q & "SQWaitStatesL.aspx?D=" & moDatabase.ID & "&T=" & oRow.Item("Wait_Type") & Q & " title=" & Q & oRow.Item("Desc") & Q & ">")
                    .Append(oRow.Item("Wait_Type"))
                    .Append("</a>")
                    .Append("</td>")

                    .Append("<td>")
                    If sAvgRequests_URL > "" Then
                        .Append("<img src=" & Q & sAvgRequests_URL & Q & " border=" & Q & "0" & Q & "/>")
                        .Append("<br/>")
                    End If
                    .Append(oRow.Item("P10_AvgRequests"))
                    .Append("</td>")

                    .Append("<td>")
                    If sAvgWaitType_URL > "" Then
                        .Append("<img src=" & Q & sAvgWaitType_URL & Q & " border=" & Q & "0" & Q & "/>")
                        .Append("<br/>")
                    End If
                    .Append(oRow.Item("P10_AvgWaitTime"))
                    .Append("</td>")

                    .Append("<td>")
                    If sAvgSignalTime_URL > "" Then
                        .Append("<img src=" & Q & sAvgSignalTime_URL & Q & " border=" & Q & "0" & Q & "/>")
                        .Append("<br/>")
                    End If
                    .Append(oRow.Item("P10_AvgSignalTime"))
                    .Append("</td>")

                    .Append("</tr>")

                    bAlternatingRow = Not bAlternatingRow
                End If
            Next

            .Append("</table>")
        End With

        Response.Write(oHTM.ToString)
    End Sub

    Private Function MakeChartString_AverageRequests(ByVal oRow As DataRow, ByVal bAlternatingRow As Boolean) As String
        Dim oURL As New StringBuilder
        Dim iMaxValue As Integer = 0

        If bAlternatingRow = True Then
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFCC")
        Else
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFFF")
        End If

        For i As Integer = 1 To 10
            oURL.Append("&DP=" & oRow.Item("P" & i & "_AvgRequests"))

            If Not oRow.Item("P" & i & "_AvgSignalTime") Is System.DBNull.Value _
                AndAlso oRow.Item("P" & i & "_AvgSignalTime") > iMaxValue Then
                iMaxValue = oRow.Item("P" & i & "_AvgSignalTime")
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If

    End Function

    Private Function MakeChartString_AvgWaitTime(ByVal oRow As DataRow, ByVal bAlternatingRow As Boolean) As String
        Dim oURL As New StringBuilder
        Dim iMaxValue As Integer = 0

        If bAlternatingRow = True Then
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFCC")
        Else
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFFF")
        End If

        For i As Integer = 1 To 10
            oURL.Append("&DP=" & oRow.Item("P" & i & "_AvgWaitTime"))

            If Not oRow.Item("P" & i & "_AvgSignalTime") Is System.DBNull.Value _
                AndAlso oRow.Item("P" & i & "_AvgSignalTime") > iMaxValue Then
                iMaxValue = oRow.Item("P" & i & "_AvgSignalTime")
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If
    End Function

    Private Function MakeChartString_AvgSignalTime(ByVal oRow As DataRow, ByVal bAlternatingRow As Boolean) As String
        Dim oURL As New StringBuilder
        Dim iMaxValue As Integer = 0

        If bAlternatingRow = True Then
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFCC")
        Else
            oURL.Append("ilc.aspx?T=L&W=60&H=20&TH=2&FG=5959FF&BG=FFFFFF")
        End If

        For i As Integer = 1 To 10
            oURL.Append("&DP=" & oRow.Item("P" & i & "_AvgSignalTime"))

            If Not oRow.Item("P" & i & "_AvgSignalTime") Is System.DBNull.Value _
                AndAlso oRow.Item("P" & i & "_AvgSignalTime") > iMaxValue Then
                iMaxValue = oRow.Item("P" & i & "_AvgSignalTime")
            End If
        Next

        If iMaxValue > 0 Then
            Return oURL.ToString
        Else
            Return ""
        End If
    End Function
End Class
